#include<bits/stdc++.h>
#define lowbit(x) ((x)&-(x))
using namespace std;
const int K = 224,D = 16,M = (1 << D);
int n,m;
int t[M|7];
void add(int p,int x)
{
	while(p <= M)
		t[p] += x,p += lowbit(p);
}
int asks(int p)
{
	int res = 0;
	while(p)
		res += t[p],p -= lowbit(p);
	return res;
}
int ask(int rk)
{
	int p = 0;
	for(int k = D;~k;k--) if(t[p + (1 << k)] < rk)
		rk -= t[p + (1 << k)],p += (1 << k);
	return p + 1;
}
int dfn[50011],dui[50011],dep[50011],idx;
int upt[50011],siz[50011],bgs[50011];
int fa[50011];
vector<int> v[50011];
void DFS(int p)
{
	dep[p] = dep[fa[p]] + 1;
	dui[dfn[p] = ++idx] = p;
	siz[p] = 1;
	for(auto i:v[p])
	{
		DFS(i);
		siz[p] += siz[i];
		if(siz[i] > siz[bgs[p]])
			bgs[p] = i;
	}
}
void DFS2(int p,int u)
{
	upt[p] = u;
	if(bgs[p])
		DFS2(bgs[p],u);
	for(auto i:v[p]) if(i != bgs[p])
		DFS2(i,i);
}
int lca(int x,int y)
{
	while(upt[x] != upt[y])
		(dep[upt[x]] > dep[upt[y]] ? (x = fa[upt[x]]) : (y = fa[upt[y]]));
	return dep[x] < dep[y] ? x : y;
}
bool b[50011];
int st[50011],top;
int a[50011];
void chk(int p)
{
	if(!b[p])
		st[++top] = p,b[p] = 1;
}
void Ist(int p)
{
	add(dfn[p],1);
	chk(p);
	int x = asks(dfn[p]);
	if(x > 1)
		chk(lca(dui[ask(x - 1)],p));
	if(x != t[M])
		chk(lca(dui[ask(x + 1)],p));
}
int l,r;
void Clear()
{
	while(l <= r)
		add(dfn[a[r--]],-1);
	while(top)
		b[st[top--]] = 0;
}
int Solve(int L,int R)
{
	while(r < R)
		Ist(a[++r]);
	int rtop = top,rl = l;
	while(l > L)
		Ist(a[--l]);
	int res = top;
	while(l < rl)
		add(dfn[a[l++]],-1);
	while(top > rtop)
		b[st[top--]] = 0;
	return res;
}
typedef struct{
	int l,r,id;
}QS;
bool cmp(QS A,QS B)
{
	return A.r < B.r;
}
vector<QS> q[50011],s;
int ans[50011];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	freopen("escape.in","r",stdin);
	freopen("escape.out","w",stdout);
	cin >> n >> m;
	for(int i = 2;i <= n;i++)
		cin >> fa[i],v[fa[i]].push_back(i);
	DFS(1),DFS2(1,1);
	for(int i = 1;i <= n;i++)
		cin >> a[i];
	for(int i = 1;i <= m;i++)
	{
		int l,r;
		cin >> l >> r;
		int L = (l / K + 1) * K;
		if(r >= L)
			q[L].push_back({l,r,i});
		else
			s.push_back({l,r,i});
	}
	for(auto i:s)
	{
		l = i.l,r = i.l - 1;
		ans[i.id] = Solve(i.l,i.r);
		Clear();
	}
	for(l = 1;l <= n;l++) if(!q[l].empty())
	{
		r = l - 1;
		sort(q[l].begin(),q[l].end(),cmp);
		for(auto i:q[l])
			ans[i.id] = Solve(i.l,i.r);
		Clear();
	}
	for(int i = 1;i <= m;i++)
		cout << ans[i] << "\n";
	return 0;
}
